﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

/*          Project build with using src from 
*   http://www.codeproject.com/KB/combobox/CheckComboBox.aspx  *
*                                                               */

namespace ComboBogCheckFlag
{
    [ToolboxBitmap(typeof(ComboBoxCheckBox), "Xoops_24" + ".ico")]

    public partial class ComboBoxCheckBox : UserControl
    {
        public ComboBoxCheckBox()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Add to ComboBox
        /// </summary>
        /// <param name="text"></param>
        /// <param name="initialCheckState"></param>
        public void AddItem(String text,bool initialCheckState)
        {
            comboBoxMainBox.Items.Add(new CheckComboBoxItem(text, initialCheckState));
        }       
        /// <summary>
        /// Invoked when the selected index is changed on the dropdown.  This sets the check state
        /// of the CheckComboBoxItem and fires the public event CheckStateChanged using the 
        /// CheckComboBoxItem as the event sender.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void comboBoxMainBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var item = (CheckComboBoxItem)comboBoxMainBox.SelectedItem;
            item.CheckState = !item.CheckState;
            if (CheckStateChanged != null)
                CheckStateChanged(item, e);
        }

        /// <summary>
        /// Invoked when the ComboBox has to render the drop-down items.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void comboBoxMainBox_DrawItem(object sender, DrawItemEventArgs e)
        {
			// make sure the index is valid (sanity check)
			if( e.Index == -1 )
			{
				return;
			}

			// test the item to see if its a CheckComboBoxItem
			if( !( comboBoxMainBox.Items[ e.Index ] is CheckComboBoxItem ) )
			{
				// it's not, so just render it as a default string
				e.Graphics.DrawString(
					comboBoxMainBox.Items[ e.Index ].ToString(),Font,Brushes.Black,new Point( e.Bounds.X, e.Bounds.Y ) );
				return;
			}

			// get the CheckComboBoxItem from the collection
			var box = (CheckComboBoxItem)comboBoxMainBox.Items[ e.Index ];

			// render it
			CheckBoxRenderer.RenderMatchingApplicationState = true;
			CheckBoxRenderer.DrawCheckBox(
				e.Graphics,
				new Point( e.Bounds.X, e.Bounds.Y ),
				e.Bounds,
				box.Text,
				Font,
				( e.State & DrawItemState.Focus ) == 0,
				box.CheckState ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal );
		}

        /// <summary>
        /// Fired when the user clicks a check box item in the drop-down list
        /// </summary>
        public event EventHandler CheckStateChanged;

    }
    
    public class CheckComboBoxItem
    {
        /// <summary>
        /// C'tor - creates a CheckComboBoxItem
        /// </summary>
        /// <param name="text">Label of the check box in the drop down list</param>
        /// <param name="initialCheckState">Initial value for the check box (true=checked)</param>
        public CheckComboBoxItem(string text, bool initialCheckState)
        {
            CheckState = initialCheckState;
            _text = text;
        }

        /// <summary>
        /// Gets the check value (true=checked)
        /// </summary>
        public bool CheckState { get; set; }

        private string _text = "";
        /// <summary>
        /// Gets the label of the check box
        /// </summary>
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        /// <summary>
        /// User defined data
        /// </summary>
        public object Tag { get; set; }

        /// <summary>
        /// This is used to keep the edit control portion of the combo box consistent
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Text;
        }

    }
}
